import request from "/utils/omsRequest";
import { contractStatus } from "/utils/dictionary";
import { showToast } from "/utils/common";
import { bus } from "/utils/core";
import { contractType, unitType } from "/utils/dictionary";
const app = getApp();

Page({
  data: {
    displayFreeServiceFlag: false,
    permissions: [], // 用户权限
    info: null,
    contractType,
    unitType, // 增值服务单位字典
    productTabIndex: 0,
    serviceTabIndex: 0,
    buyServiceTabIndex: 0,
    depositTabIndex: 0,
    refundTabIndex: 0,
    contractStatus, // 合同审核状态
    visibleDepositModal: false, // 收款申请modal显示
    visibleRefundModal: false, // 退款申请modal显示
    currentItem: null // 页面向收退款modal传输的数据
  },
  onUnload() {
    bus.remove("contractInfoFile")
  },
  async onLoad(query) {
    this.setData({
      permissions: app.globalData.permissions,
      displayFreeServiceFlag: app.globalData.permissions.includes(370)
    });
    bus.subscribe("contractInfoFile", (files) => {
      if (files.length > 0) {
        dd.showLoading({
          content: '加载中...'
        });
        request({
          url: `/ajax/contract/addEnclosure/${query.id}`,
          method: "post",
          data: {
            id: +query.id,
            sysEnclosureList: files
          },
          headers: {
            "Content-Type": "application/json;utf-8"
          }
        }).then(() => {
          dd.hideLoading();
          this.data.info.sysEnclosureList.push(...files);
          this.setData({
            info: this.data.info
          });
          showToast({
            type: "success",
            msg: "上传成功!"
          });
        })
      }
    })
    dd.showLoading({
      content: '加载中...'
    });
    await this.queryContractInfo(query.id)
    if (this.data.displayFreeServiceFlag) {
      request({
        url: "/ajax/contractFreeService/list",
        data: {
          contractGroupId: query.id
        }
      }).then(res => {
        this.setData({
          "info.freeServiceList": res.voList.filter(item => item.freeServiceType === 'FREE'),
          "info.buyServiceList": res.voList.filter(item => item.freeServiceType === 'BUY'),
        });
        this.setData({
          "info.freeServiceAmount": this.data.info.freeServiceList.reduce((total, cur) => cur.processResult === 'AGREE' ? total + cur.totalPrice : total, 0),
          "info.buyServiceAmount": this.data.info.buyServiceList.reduce((total, cur) => cur.processResult === 'AGREE' ? total + cur.totalPrice : total, 0)
        })
      })
    }
  },
  // 控制收款modal显示隐藏
  onDepositShow(e) {
    this.setData({
      visibleDepositModal: e.target.dataset.show,
      currentItem: e.target.dataset.show ? {
        product: e.target.dataset.product,
        contractGroupId: this.data.info.id
      } : null
    })
    if (e.target.dataset.init) {
      this.queryContractInfo(this.data.info.id)
    }
    if (e.target.dataset.product) {
      bus.publish('queryDepositRecords', e.target.dataset.product)
    }
  },
  onRefundShow(e) {
    this.setData({
      visibleRefundModal: e.target.dataset.show,
      currentItem: e.target.dataset.show ? {
        product: e.target.dataset.product,
        contractGroupId: this.data.info.id
      } : null
    })
    if (e.target.dataset.init) {
      this.queryContractInfo(this.data.info.id)
    }
    if (e.target.dataset.product) {
      bus.publish('queryRefundRecords', e.target.dataset.product)
    }
  },
  onTabSelect(e) {
    let obj = {};
    obj[e.target.dataset.tabIndexKey] = e.currentTarget.dataset.index;
    this.setData(obj);
  },
  queryContractInfo(id) {
    return new Promise((resolve, reject) => {
      dd.showLoading({
        content: '加载中...'
      });
      request({
        url: "/ajax/contract/findContract",
        data: {
          id: id
        }
      }).then(res => {
        dd.hideLoading();
        this.setData({
          info: res.vo
        });
        resolve()
      }).catch(() => {
        reject()
      })
    })
  }
});
